package com.qf.entity;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 运行时日志实体类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class RunLogEntity implements Serializable {

    //请求标识
    private String requestId;
    //请求url
    private String url;
    //处理类全路径
    private String className;
    //处理方法
    private String methodName;
    //参数列表
    private Map<String, String> params = new HashMap<>();
    //返回值
    private String result;
    //异常信息
    private String throwInfo;
    //排序
    private Integer order;
//    //执行的
//    private String sql;
//    //sql语句的参数值
//    private String sqlParam;
    //发生时间
    private String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());


    public void putParams(String paramsType, String paramsValue){
        params.put(paramsType, paramsValue);
    }

    public void setThrowInfo(Throwable t){
        setThrowInfo(getExceptionCode(t));
    }

    public RunLogEntity setThrowInfo(String t){
        throwInfo = t;
        return this;
    }

    public String toJson() throws UnsupportedEncodingException {

        StringBuilder sb = new StringBuilder();
        sb.append("{");

        sb.append("\"requestId\":\"").append(requestId).append("\",");
        sb.append("\"url\":\"").append(url).append("\",");
        sb.append("\"className\":\"").append(URLEncoder.encode(className, "utf-8")).append("\",");
        sb.append("\"methodName\":\"").append(methodName).append("\",");
        sb.append("\"params\":\"").append(URLEncoder.encode(JSONObject.toJSONString(params), "utf-8")).append("\",");
        sb.append("\"result\":\"").append(result != null ? URLEncoder.encode(result, "utf-8") : result).append("\",");
        sb.append("\"throwInfo\":\"").append(throwInfo).append("\",");
//        sb.append("\"t\":\"").append(getException(t)).append("\",");
        sb.append("\"order\":").append(order).append(",");
//        sb.append("\"sql\":\"").append(sql).append("\",");
//        sb.append("\"sqlParam\":\"").append(sqlParam).append("\",");
        sb.append("\"time\":\"").append(time).append("\"");

        sb.append("}");

        return sb.toString();
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("序列：").append(order).append("\n");
        sb.append("请求标识：").append(requestId).append("\n");
        sb.append("请求url：").append(url).append("\n");
        sb.append("处理类全路径：").append(className).append("\n");
        sb.append("处理方法：").append(methodName).append("\n");
        sb.append("参数列表：").append("\n");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            sb.append(entry.getKey()).append("--").append(entry.getValue()).append("\n");
        }
        sb.append("返回值：").append(result).append("\n");
        sb.append("---------------------------------");
        return sb.toString();
    }

    public static String getExceptionCode(Throwable e) {
        try {
            return URLEncoder.encode(getException(e), "utf-8");
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String getException(Throwable e) {
        if(e == null){
            return null;
        }
        StackTraceElement[] ste = e.getStackTrace();
        StringBuffer sb = new StringBuffer();
        sb.append(e.getMessage() + " ");
        for (int i = 0; i < ste.length; i++) {
            sb.append("  " + ste[i].toString() + "\n");
        }
        return sb.toString();
    }
}

